<?php
/**
 * 
 * Timer
 * @author mrpie
 * 
 */
class Timer {
	private $Start;
	private $Delta;
	private $End;
	private $times = array(
		'S' => 1,
		'M' => 60,
		'H' => 3600,
		'D' => 86400,
		'W' => 604800
	);
	
	function __construct() {
		$this->Start = NULL;
		$this->Delta = NULL;
		$this->End   = NULL;
	}
	function Start() {
		$this->Delta = NULL;
		$this->End   = NULL;
		$this->Start = $this->getTime();
		return $this->Start;
	}
	function getStart() {return $this->Start;}
	function getDelta() {
		if (is_null($this->End)) {
			$this->Delta = $this->getTime() - $this->Start;
		} 
		return $this->Delta;
	}
	function End() {
		$this->End = $this->getTime();
		$this->Delta = $this->End - $this->Start;
	}
	function getTime() {
		return time();
	}
	function getNewTime($TimeStr = '') {
		return $this->getStart() + $this->ParseTime($TimeStr);
	}
	function ParseTime($TimeStr = '') {
		$Time = 0;
		if ($TimeStr != '') {
			if (preg_match_all('#([0-9]+)([A-Z]+)#si', $TimeStr, $match)) {
				for ($i = 0; isset($match[1][$i]); ++$i) {
					$Time += $this->times[$match[2][$i]]*$match[1][$i];
				}
			}
		}
		return $Time;
	}
}
?>